package com.foreknow.service.impl;

import com.foreknow.dao.impl.BusinessDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Business;
import com.foreknow.service.BusinessService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BusinessServiceImpl implements BusinessService {
    private BusinessDaoImpl businessDao = new BusinessDaoImpl();
    @Override
    public boolean save(Business business) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        businessDao.setConnection(conn);
        boolean isRigth = false;
        try {
            isRigth = businessDao.save(business);
            //dml操作需要提交事物
            conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public List<Business> getQueryAll() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        businessDao.setConnection(conn);
        List<Business> list = new ArrayList<>();
        try {
            List<Object> objects = businessDao.getQueryAll();
            for (Object o :
                    objects) {
                Business business = (Business) o;
                list.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int Update(Business business, int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        businessDao.setConnection(conn);
        try {
            int i = businessDao.Update(business,id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Business> getQueryByName(String businessName) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        businessDao.setConnection(conn);
        List<Business> list = new ArrayList<>();
        try {
            List<Object> objects = businessDao.getQueryByName(businessName);
            for (Object o :
                    objects) {
                Business business = (Business) o;
                list.add(business);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public boolean deleteById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        //调用的是BaseDAO中的setConnection(conn)方法
        businessDao.setConnection(conn);
        try {
            boolean b = businessDao.deleteById(id);
            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
